﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _11.AddPolynomials
{
    class Program
    {
        static int[] Input(out int pow, int number)
        {
            Console.Write("Enter the heighest power of x for polynome {0}: ", number);
            int power = int.Parse(Console.ReadLine());
            int[] polynome = new int[power + 1];
            for (int index = 0; index <= power; index++)
            {
                polynome[power - index] = int.Parse(Console.ReadLine());
            }
            pow = power;
            return polynome;
        }

        static int[] SumOfPolynoms(int[] firstPolinome, int[] secondPolinome)
        {
            int[] result = new int[Math.Max(firstPolinome.Length, secondPolinome.Length)];
            int index;
            for (index = 0; index < Math.Min(firstPolinome.Length, secondPolinome.Length); index++)
            {
                result[index] = firstPolinome[index] + secondPolinome[index];
            }
            for (; index < Math.Max(firstPolinome.Length, secondPolinome.Length); index++)
            {
                result[index] = firstPolinome.Length > secondPolinome.Length ? firstPolinome[index] : secondPolinome[index];
            }

            return result;
        }

        static void Print(int[] arr)
        {
            for (int i = arr.Length - 1; i >= 0; i--)
            {
                Console.Write("{0}x^{1} ", arr[i], i);
            }
        }

        static void Main(string[] args)
        {
            int power1;
            int[] polynome1 = Input(out power1, 1);
            int power2;
            int[] polynome2 = Input(out power2, 2);

            int[] sum = SumOfPolynoms(polynome1, polynome2);
            Print(sum);
        }
    }
}